﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadProps
{
    class Prop<T> : IProp<T>
    {
        readonly PropId id;
        readonly object owner;

        public Prop(PropId id, object owner)
        {
            this.id = id.ThrowIfNull("id");
            this.owner = owner.ThrowIfNull("owner");

            if (typeof(T) == typeof(string))
            {
                Value = (T)(object)(string.Empty);
            }
        }

        public PropId PropId { get { return id; } }

        T value;
        public T Value
        {
            get { return value; }
            set
            {
                if(object.Equals(this.value, value))
                {
                    return;
                }

                var temp = this.value;
                this.value = value;

                var args = new PropChangedEventArgs(this, temp, value);
                PropChanged.Raise(this, args);
            }
        }

        object IProp.Value
        {
            get { return Value; }
            set { Value = (T)value; }
        }

        public Type Type { get { return typeof(T); } }

        public object Owner { get { return owner; } }

        public event EventHandler<PropChangedEventArgs> PropChanged;
    }
}
